package com.eshop.modules.shop.rest;

import java.lang.invoke.SerializedLambda;
import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.modules.shop.domain.SystemStore;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.shop.domain.SystemStoreStaff;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.modules.shop.service.dto.SystemStoreStaffDto;
import com.eshop.modules.shop.service.dto.SystemStoreStaffQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.shop.service.SystemStoreService;
import com.eshop.modules.shop.service.SystemStoreStaffService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "门店店员管理" })
@RestController
@RequestMapping({ "/api/yxSystemStoreStaff" })
public class SystemStoreStaffController
{
    private final SystemStoreStaffService systemStoreStaffService;
    private final SystemStoreService systemStoreService;
    private final IGenerator generator;
    
    public SystemStoreStaffController(final SystemStoreService systemStoreService, final SystemStoreStaffService systemStoreStaffService, final IGenerator generator) {
        this.systemStoreService = systemStoreService;
        this.systemStoreStaffService = systemStoreStaffService;
        this.generator = generator;
    }
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('yxSystemStoreStaff:list')")
    public void download(final HttpServletResponse response, final SystemStoreStaffQueryCriteria criteria) throws IOException {
        this.systemStoreStaffService.download(this.generator.convert(this.systemStoreStaffService.queryAll(criteria), (Class)SystemStoreStaffDto.class), response);
    }
    
    @GetMapping
    @Log("查询门店店员")
    @ApiOperation("查询门店店员")
    @PreAuthorize("@el.check('yxSystemStoreStaff:list')")
    public ResponseEntity<Object> getYxSystemStoreStaffs(final SystemStoreStaffQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.systemStoreStaffService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @PostMapping
    @Log("新增门店店员")
    @ApiOperation("新增门店店员")
    @PreAuthorize("@el.check('yxSystemStoreStaff:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final SystemStoreStaff resources) {
        final SystemStore systemStore = (SystemStore)this.systemStoreService.getOne(Wrappers.<SystemStore>lambdaQuery().eq(SystemStore::getId, resources.getStoreId()));
        resources.setStoreName(systemStore.getName());
        return new ResponseEntity(this.systemStoreStaffService.save(resources), HttpStatus.CREATED);
    }
    
    @PutMapping
    @Log("修改门店店员")
    @ApiOperation("修改门店店员")
    @PreAuthorize("@el.check('yxSystemStoreStaff:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final SystemStoreStaff resources) {
        final SystemStore systemStore = (SystemStore)this.systemStoreService.getOne(Wrappers.<SystemStore>lambdaQuery().eq(SystemStore::getId, resources.getStoreId()));
        resources.setStoreName(systemStore.getName());
        this.systemStoreStaffService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除门店店员")
    @ApiOperation("删除门店店员")
    @PreAuthorize("@el.check('yxSystemStoreStaff:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Integer[] ids) {
        this.systemStoreStaffService.removeByIds((Collection)new ArrayList(Arrays.asList(ids)));
        return new ResponseEntity(HttpStatus.OK);
    }
}
